<!--新增实验环节-->
<template>
  <div>
    <el-dialog :title="title" v-model="linkDialogVisible" :before-close="closeDialog" v-on:open="loadData" :close-on-click-modal="false">
      <el-form :model="form" :inline="true" ref="form" label-width="100px">
        <el-form-item label="环节名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="环节类型">
          <el-select v-model="form.linkTypeId" placeholder="选择环节类型">
            <el-option
              v-for="item in typeList"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="周期">
          <el-input v-model="form.cycles"></el-input>
        </el-form-item>
        <el-form-item label="PKTime">
          <el-input v-model="form.pkTime"></el-input>
        </el-form-item>
        <br/>
        <el-form-item label="时效约束">
          <el-radio class="radio" v-model="form.timeRestrain" :label="1">是</el-radio>
          <el-radio class="radio" v-model="form.timeRestrain" :label="0">否</el-radio>

          <template v-if="form.timeRestrain==1">
            <el-select v-model="form.relationProcessId" placeholder="请选择试验节点">
              <el-option label="无" value=""></el-option>
              <el-option
                v-for="item in referenceList"
                :label="'周期'+item.cycles+'-'+item.name"
                :value="item.id">
              </el-option>
            </el-select>
            <el-input v-model="form.nextHours" style="width: 85px"></el-input>
            小时
          </template>
        </el-form-item>
        <el-form-item class="button-row" style="text-align: right;width:100%;">
          <el-button type="primary" @click.native.prevent="handleSubmit" :disabled="editClick">{{addBtnTxt}}</el-button>
          <el-button @click.native.prevent="handleReset">重置</el-button>
          <el-button @click.native.prevent="closeDialog">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
  import http from '../../../api/http'

  var model = {
    name: null,
    cycles: null,
    pkTime: null,
    linkTypeId: null,
    projectId: '',
    timeRestrain: 0,
    nextHours: 0,
    reference: 0,
    relationProcessId: null
  };
  export default {
    data() {
      return {
        editClick:false,
        form: Object.assign({}, model),
        //存储环节类型
        typeList: [],
        //存储引用类型
        referenceList: [],
        title: '',
        addBtnTxt: ''
      }
    },
    props: {
      linkDialogVisible: {
        type: Boolean,
        default: function () {
          return false
        }
      },
      projectId: {
        type: String
      },
      processId: {
        type: String
      }
    },
    computed: {
    },
    methods: {
      handleReset() {
        this.form.resetFields();
      },
      loadData() {
        var vm = this;
        if (this.processId == null || vm.processId == undefined || vm.processId == '') {
          vm.addBtnTxt = '立即创建';
          vm.title = '新建项目流程';
          vm.form = Object.assign({}, model);
        } else {
          this.form.id = this.processId;
          http.postData(vm, "/pt/process/detail/" + vm.form.id, {}, function (resp) {
            var ret = resp.body;
            if (ret.code === '111') {
              vm.form = (ret.value);
              if (vm.form.relationProcessId == undefined) {
                vm.form.relationProcessId = null;
              }
            } else {
              vm.$notify.error({title: '操作失败', message: ret.msg});
            }
          });
          vm.addBtnTxt = '保存修改';
          vm.title = "修改项目流程";
        }
        http.postData(vm, "/pt/process/findRelationList/" + vm.projectId + "-" + vm.form.id, {}, function (resp) {
          var ret = resp.body;
          if (ret.code === '111') {
            vm.referenceList = ret.value;
          } else {
            return false
          }
        });
        // 加载实验环节类型
        http.postData(vm, "/pt/linkType/listByReference", {}, function (resp) {
          var ret = resp.body;
          if (ret.code === '111') {
            vm.typeList = ret.value;
          } else {
            return false
          }
        });
      },
      closeDialog() {
          this.editClick = false;
        this.$parent.closeLinkDialog();
      },
      handleSubmit(event) {
        var vm = this;
        this.$refs.form.validate(function (valid) {
          if (valid) {
            vm.editClick = true;
            vm.form.projectId = vm.projectId;
            http.postData(vm, "/pt/process/save", {json: JSON.stringify(vm.form)}, function (resp) {
              var ret = resp.body;
              if (ret.code === '111') {
                vm.$notify({title: '操作提示', message: ret.msg, type: 'success'});
                vm.$parent.onSubmit();
                vm.$parent.closeLinkDialog();
              } else {
                  vm.editClick = false;
                vm.$notify.error({title: '操作失败', message: ret.msg});
              }
            });
          } else {
            return false
          }
        })
      }
    },
    created() {
      var vm = this;
      http.postData(vm, "/pt/linkType/list", {}, function (resp) {
        var ret = resp.body;
        if (ret.code === '111') {
          vm.typeList = ret.value;
        } else {
          return false
        }
      });
    }
  }
</script>

